package com.xiaowei.gym.gymjsp.ware.servlet;

import com.xiaowei.gym.gymjsp.ware.dao.UserDao;
import com.xiaowei.gym.gymjsp.ware.dao.impl.USerDaoImpl;
import com.xiaowei.gym.gymjsp.ware.entity.AdminEntity;
import com.xiaowei.gym.gymjsp.ware.entity.GymMenuEntity;
import com.xiaowei.gym.gymjsp.ware.entity.UserEntity;
import com.xiaowei.gym.gymjsp.ware.service.AdminService;
import com.xiaowei.gym.gymjsp.ware.service.GymMenuService;
import com.xiaowei.gym.gymjsp.ware.service.UserService;
import com.xiaowei.gym.gymjsp.ware.service.impl.AdminServiceImpl;
import com.xiaowei.gym.gymjsp.ware.service.impl.GymMenuServiceImpl;
import com.xiaowei.gym.gymjsp.ware.service.impl.UserServiceImpl;
import com.xiaowei.gym.gymjsp.ware.util.Constants;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

@WebServlet("/Login")
public class LoginServlet extends HttpServlet {
    private final AtomicInteger loginAttemptCounter = new AtomicInteger(0);
    private AdminService adminService;
    private GymMenuService gymMenuService;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        System.out.println("req"+req.toString());
        todashboad(req,resp);

    }

    @Override
    public void init() throws ServletException {
        adminService = new AdminServiceImpl();
        gymMenuService = new GymMenuServiceImpl();
    }

    private void todashboad(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        if (loginAttemptCounter.incrementAndGet() > 3) {
            response.sendRedirect("login.jsp?error=Too many failed login attempts.");
            return;
        }
        System.out.println(username);
        System.out.println(password);
        AdminEntity adminEntity =  adminService.validate(username, password);
        if (adminEntity != null) {
            if (request.getAttribute("error") != null) {
                request.removeAttribute("error");
            }
            List<GymMenuEntity> allMenus = gymMenuService.getAllMenus();
            System.out.println("allMenus:"+allMenus);
            HttpSession session = request.getSession();
            session.setAttribute(Constants.USER_SESSION,adminEntity);
            System.out.println(allMenus);
            if (allMenus.size() > 0){
                session.setAttribute("menus",allMenus);
            }
            // 重置计数器
            loginAttemptCounter.set(0);
            response.sendRedirect("gymjsp/dashboard.jsp");
        } else {
            System.out.println("错误消息");
            tologin(request,response); // 登录失败返回登录页面
        }
    }

    private void tologin(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException {
        request.setAttribute("error", "Invalid username or password");
        Object error = request.getAttribute("error");
        System.out.println("错误消息:"+error);
        request.getRequestDispatcher("login.jsp").forward(request, response);
    }

}
